import java.util.*;

public class BinaryTree {

    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree(){
        TreeNode A=new TreeNode('A');
        TreeNode B=new TreeNode('B');
        TreeNode C=new TreeNode('C');
        TreeNode D=new TreeNode('D');
        TreeNode E=new TreeNode('E');
        TreeNode F=new TreeNode('F');
        TreeNode G=new TreeNode('G');
        TreeNode H=new TreeNode('H');

        A.left=B;
        A.right=C;
        B.left=D;
        B.right=E;
        C.left=F;
        C.right=G;
        //E.right=H;

        return A;//返回根节点
    }

    public void preOrder(TreeNode root){
        if(root==null) return;
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }

    public void inOrder(TreeNode root){
        if(root==null) return;
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    public void postOrder(TreeNode root){
        if(root==null) return;
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }

    /*将前序遍历的结果存储在 list 当中*/
    /*List<TreeNode> ret=new ArrayList<>();
    public List<TreeNode> preOrder1(TreeNode root){
        if(root==null) return ret;
        ret.add(root);
        preOrder1(root.left);
        preOrder1(root.right);
        return ret;
    }*/

    public List<TreeNode> preOrder2(TreeNode root){
        List<TreeNode> ret=new ArrayList<>();
        if(root==null) return ret;
        ret.add(root);
        List<TreeNode> leftTree=preOrder2(root.left);
        ret.addAll(leftTree);
        List<TreeNode> rightTree=preOrder2(root.right);
        ret.addAll(rightTree);
        return ret;
    }


    public int size=0;


    // 获取树中节点的个数
    int sizeNode(TreeNode root){
        if(root==null) return 0;
        size++;
        sizeNode(root.left);
        sizeNode(root.right);
        return size;
    }

    int sizeNode2(TreeNode root){
        if(root==null) return 0;
        return sizeNode2(root.left)+sizeNode2(root.right)+1;
    }

    // 获取叶子节点的个数
    public int sizeLeaf=0;

    int getLeafNodeCount(TreeNode root){
        if(root==null) return 0;
        if(root.left==null && root.right==null) sizeLeaf++;
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
        return sizeLeaf;
    }

    int getLeafNodeCount2(TreeNode root){
        if(root==null) return 0;
        if(root.left==null && root.right==null) return 1;
        return getLeafNodeCount2(root.left)+ getLeafNodeCount2(root.right);
    }

    // 获取第K层节点的个数
    int getKLevelNodeCount(TreeNode root,int k){
        if(root==null) return 0;
        if(k==1) return 1;
        return getKLevelNodeCount(root.left,k-1)+getKLevelNodeCount(root.right,k-1);
    }
    // 获取二叉树的高度
    int getHeight(TreeNode root){
        if(root==null) return 0;
        int leftHeight=getHeight(root.left);
        int rightHeight=getHeight(root.right);

        return leftHeight > rightHeight ? leftHeight+1 : rightHeight+1;
    }

    //超出时间限制 ：1、死循环 2、递归条件错误 3、超时但无错误（有重复计算）
    int getHeight2(TreeNode root){
        if(root==null) return 0;
        return getHeight(root.left) >getHeight(root.right) ? getHeight(root.left)+1 : getHeight(root.right)+1;
    }

    // 检测值为value的元素是否存在
    boolean find(TreeNode root, char key){
        if(root==null) return false;

        if(root.val==key) return true;

        boolean leftVal=find(root.left,key);
        if(leftVal==true) return true;

        boolean rightVal=find(root.right,key);
        if(rightVal==true) return true;

        return false;
    }
    //层序遍历
    void levelOrder(TreeNode root){
         if(root==null) return;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur=queue.poll();
            System.out.print(cur.val+" ");
            if(cur.left!=null) queue.offer(cur.left);
            if(cur.right!=null) queue.offer(cur.right);
        }
    }

    List<List<TreeNode>> levelOrder1(TreeNode root){
        List<List<TreeNode>> ret=new ArrayList<>();
        if(root==null) return null;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()) {
            //求一下当前队列的大小
            int size = queue.size();
            List<TreeNode> tmp=new ArrayList<>();
            while (size != 0) {
                TreeNode cur = queue.poll();
                tmp.add(cur);
                size--;
                if (cur.left != null) queue.offer(cur.left);
                if (cur.right != null) queue.offer(cur.right);
            }
            ret.add(tmp);
        }
        return ret;
    }


    // 判断一棵树是不是完全二叉树
    boolean isCompleteTree(TreeNode root){
        if(root==null) return true;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()){
            TreeNode cur=queue.poll();
            if(cur!=null){
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else{
                break;
            }
        }

        while (!queue.isEmpty()){
            TreeNode cur=queue.poll();
            if(cur!=null) return false;
        }
        return true;
    }

    //二叉树的最近公共祖先
    public TreeNode lowestCommonAncestor(TreeNode root,TreeNode p,TreeNode q){
        if(root==null) return null;
        if(root==p || root==q) return root;
        //要么就在左右两边，要么就一边有一边没有
        TreeNode leftTree=lowestCommonAncestor(root.left,p,q);//要是root.left=root，则直接把root.left返回给leftTree,就不会再执行right
        TreeNode rightTree=lowestCommonAncestor(root.right,p,q);
        if(leftTree!=null && rightTree!=null){
            //左右两边分别找到了p q
            return root;
        }else if(leftTree!=null){
            return leftTree;
        }else {
            return rightTree;
        }
    }

    //找 根节点 到 node节点 的路径
    private boolean getPath(TreeNode root,TreeNode node,Stack<TreeNode> stack){
        if(root==null || node==null) return false;
        //root!=null
        stack.push(root);
        if(root==node) return true;//要是不等于就在左树以同样的方式进行递归

        boolean flg=getPath(root.left,node,stack);
        if(flg) return true;
        boolean flg2=getPath(root.right,node,stack);
        if(flg2) return true;

        stack.pop();//root左右两边都没找到，则它一定不是路径上的节点，则弹出
        return false;
    }

    public TreeNode lowestCommonAncestor1(TreeNode root,TreeNode p,TreeNode q) {
        if (root == null) return null;

        //先找到 root 分别到 p和q 的路径
        Stack<TreeNode> stackP = new Stack<>();
        Stack<TreeNode> stackQ = new Stack<>();
        getPath(root, p, stackP);
        getPath(root, q, stackQ);

        //对栈的操作
        int sizeP = stackP.size();
        int sizeQ = stackQ.size();

        if (sizeP > sizeQ) {
            int size = sizeP - sizeQ;
            while (size != 0) {
                stackP.pop();
                size--;
            }
        } else {
            int size = sizeQ - sizeP;
            while (size != 0) {
                stackQ.pop();
                size--;
            }
        }
        while (!stackP.isEmpty() && !stackQ.isEmpty()) {
            if (stackP.peek().equals(stackQ.peek())) {
                return stackP.peek();
            }
            stackQ.pop();
            stackP.pop();
        }
        return null;
    }
}

























